package com.dongdongshop.service.impl;

import com.dk.util.PageResult;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class SpecificationServiceimpl  implements SpecificationService {

    @Autowired
    TbSpecificationMapper tsm;

    @Autowired
    TbSpecificationOptionMapper tom;


    //分页查询全部
    @Override
    public PageResult getall(Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<TbSpecification> ls = tsm.selectByExample(null);
        PageInfo info=new PageInfo(ls);
        PageResult p=new PageResult(pagenum,pagesize,info.getList(),info.getTotal());
        return p;
    }

    


    //增加
    @Override
    public int addspecifs(TbSpecification tb, List<TbSpecificationOption> tbSpecificationOptions) {
        tsm.addspecif(tb);
        Long id = tb.getId();//显增加一个表，获取增加成功后返回的主键
        tom.addspecfoptionorder(id,tbSpecificationOptions);
        return 1;
    }

    
    //修改回显
    @Override
    public SpecVo findSpecbyid(Long id) {
        TbSpecification tbSpecification = tsm.selectByPrimaryKey(id);

        TbSpecificationOptionExample toe=new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = toe.createCriteria();
        criteria.andSpecIdEqualTo(id);//根据SpecId查询数据，返回集合
        List<TbSpecificationOption> ls = tom.selectByExample(toe);

        SpecVo vo=new SpecVo();//将获取到的值先传入vo类中
        vo.setOptionList(ls);
        vo.setSpecification(tbSpecification);
        return vo;
    }


    //修改
    @Override
    public int updatespec(TbSpecification tb, List<TbSpecificationOption> tbSpecificationOptions) {
        tsm.updateByPrimaryKey(tb);
        tom.deletebyid(tb.getId());
        tom.addspecfoptionorder(tb.getId(),tbSpecificationOptions);
        return 1;
    }


    //删除
    @Override
    public int deleteid(Long id) {
        tsm.deleteByPrimaryKey(id);
        tom.deletebyid(id);
        return 1;
    }

    //查询全部
    @Override
    public List<TbSpecification> getlist() {
        return tsm.selectByExample(null);
    }
}
